
exports.GAPersistenceManager = Core.Class.singleton ({

	loadResources: function(resources, progressCallback, doneCallback, errorCallback, customPath) {
		this._resources = resources;
		this._progressCallback = progressCallback;
		this._doneCallback = doneCallback;
		this._errorCallback = errorCallback;
		this._totalResources = this._resources.length;
		this._downloadCounter = 0;
		this._objects = {};
		
		this._customPath = null;
		if ((customPath != null) && (customPath != undefined)){
			this._customPath = customPath;
		}
		
		this._obj = new GAPersistedObject(this._resources[this._downloadCounter], this.loadComplete.bind(this), customPath);
	},
	
	loadComplete: function(resourceName,error) {
		if(error)
		{
			if(this._errorCallback)
			{
				this._errorCallback(resourceName);
				return;
			}
		}
		this._objects[this._resources[this._downloadCounter]] = this._obj;
		if (this._downloadCounter < this._totalResources - 1) {
			this._obj = new GAPersistedObject(this._resources[++this._downloadCounter], this.loadComplete.bind(this), this._customPath);
			this._progressCallback(this._downloadCounter, this._totalResources);
		}
		else {
			this._progressCallback(this._downloadCounter+1, this._totalResources);
			this._doneCallback();
		}
	},
	
	unLoadResource: function(resourceName) {
		if(this._objects[resourceName])
		{
			console.log("unloading the object "+resourceName);
			this._objects[resourceName].destroy();
			this._objects[resourceName] = null;
		}
		else
		{
			console.log("ERROR unloading the object: No resource of this name found");
		}
	},
	
	getResource: function(resourceName) {
		return this._objects[resourceName];
	},
	
	destroy: function() {
		var len = this._objects.length;
		for (var i = 0; i < len; i++) {
			this._objects[i].destroy();
			this._objects[i] = null;
		}
		delete this._objects;
		delete this._resources;
		this._progressCallback = null;
		this._doneCallback = null;
	}
	
	
});